// PURPOSE: 

clear all

// SET MACROS
global Input  "Y:/limited/Michigan_CTE/funding_change/data_raw"
global Output "Y:/limited/Michigan_CTE/funding_change/data_derived"

local vars ric year grade cipcode pathway fedcluster adv_p4 total_segments segment13 total_mins assess* psn
set seed 20150605

// LOAD DATA
use `vars' using ${Input}/cte_program_enrollment.dta

*Cooking gets relabeled
replace cipcode = "12.0500" if cipcode == "12.9999"

* Family and consumer science is not federally recognized and is new in 2019
drop if cipcode=="19.0000"

// RENAME VARIABLES
rename ric student

// FLAG PROGRAMS WITH MANDATED TESTS, 2014-2017
gen test_cip = inlist(cipcode , "43.0100","47.0603", "47.0604", "47.0613" , "51.0000", "52.0299", "52.0800", "52.1999")
gen assessment2 = 0
replace assessment2 = 2 if test_cip==0
replace assessment2 = 1 if !inlist(assess_score,.,0)

//Segments
replace total_segments = 13 if total_segments==12 & inrange(segment13,2,4)

//Courses
merge m:1 student psn using ${Output}/course_count.dta, keep(1 3) nogen
recode course_count total_semesters (.=2)
replace course_count = 6 if course_count>6

* Courses to complete
bys psn: gegen comp_course = min(cond(inrange(year,2010,2014)&adv_p4==3 ,course_count,.))

gen course_complete = adv_p4==3 & (course_count>= comp_course) if comp_course!=.
gen courseless_complete = adv_p4==3 & (course_count< comp_course) if comp_course!=.
drop psn


//Rank Factor
merge m:1 cipcode using ${Input}/cte_2015_ranklist.dta, nogen
recode factor .=1
ren factor rankfactor

//Cost Factor
merge m:1 cipcode using ${Input}/cte_2015_costlist.dta,  //gen _merge to use in the drop command later
recode costfactor .=1


// CLEAN VARIABLES

// Drop CTE enrollments before ninth grade or  >3 years after ninth grade
merge m:1 student using $Output/g9_cohorts.dta, keepusing(cohort9) keep(3) nogen
drop if year > (cohort9 + 3)  	// Drops 9636 observations
drop if year < (cohort9)  		// Drops 46 observations


// Drop if missing grade
drop if missing(grade)  		// Drops 61 observations

// CTE completion, concentration, and participation
bys student (year): gegen cte_adv_high = max(adv_p4)


gen cte_comp = (cte_adv_high == 3)

gen cte_conc_comp = (cte_adv_high >= 2)
gen cte_any = (cte_adv_high >= 1)

gen cte_conc = (cte_adv_high == 2)
gen cte_part = (cte_adv_high == 1)

gen cte_completion_status = cte_adv_high

by student: gegen cte_any_course_complete = max(course_complete)
by student: gegen cte_any_courseless_complete= max(courseless_complete)


// CTE COMPLETION, CONCETRATION, AND PARTICIPATION BY CLUSTER
forvalues x = 1/16 {
  bys student (year cipcode): gegen cte_adv_high_cluster`x' = max(adv_p4 * (fedcluster == `x'))
  gen cte_comp_cluster`x'      = (cte_adv_high_cluster`x' == 3)
  gen cte_conc_comp_cluster`x' = (cte_adv_high_cluster`x' >= 2 & !missing(cte_adv_high_cluster`x'))
  gen cte_any_cluster`x'       = (cte_adv_high_cluster`x' >= 1 & !missing(cte_adv_high_cluster`x'))
  gen cte_conc_cluster`x'      = (cte_adv_high_cluster`x' == 2)
  gen cte_part_cluster`x'      = (cte_adv_high_cluster`x' == 1)
}

// CTE COMPLETION, CONCETRATION, AND PARTICIPATION BY PROGRAM
glevelsof pathway, local(levels)
foreach x of local levels {
  by student (year cipcode): gegen cte_adv_high_prog`x' = max(adv_p4 * (pathway == `x'))
  gen cte_comp_prog`x'      = (cte_adv_high_prog`x' == 3)
  gen cte_conc_comp_prog`x' = (cte_adv_high_prog`x' >= 2 & !missing(cte_adv_high_prog`x'))
  gen cte_any_prog`x'       = (cte_adv_high_prog`x' >= 1 & !missing(cte_adv_high_prog`x'))
  gen cte_conc_prog`x'      = (cte_adv_high_prog`x' == 2)
  gen cte_part_prog`x'      = (cte_adv_high_prog`x' == 1)
}

// CTE COMPLETION, CONCETRATION, AND PARTICIPATION BY STATE RANK LIST FACTOR
glevelsof rankfactor, local(levels)
foreach y of local levels {
  local x  = round(`y',1)	
  by student (year cipcode): gegen cte_adv_high_fact`x' = max(adv_p4 * (rankfactor == `y'))
  gen cte_comp_rankfact`x'      = (cte_adv_high_fact`x' == 3)
  gen cte_conc_comp_rankfact`x' = (cte_adv_high_fact`x' >= 2 & !missing(cte_adv_high_fact`x'))
  gen cte_any_rankfact`x'       = (cte_adv_high_fact`x' >= 1 & !missing(cte_adv_high_fact`x'))
  gen cte_conc_rankfact`x'      = (cte_adv_high_fact`x' == 2)
  gen cte_part_rankfact`x'      = (cte_adv_high_fact`x' == 1)
	drop cte_adv_high_fact`x'
}


// CTE COMPLETION, CONCETRATION, AND PARTICIPATION BY STATE COST LIST FACTOR
glevelsof costfactor, local(levels)
foreach y of local levels {
  local x  = round(`y',1)	
  by student (year cipcode): gegen cte_adv_high_fact`x' = max(adv_p4 * (costfactor == `y'))
  gen cte_comp_costfact`x'      = (cte_adv_high_fact`x' == 3)
  gen cte_conc_comp_costfact`x' = (cte_adv_high_fact`x' >= 2 & !missing(cte_adv_high_fact`x'))
  gen cte_any_costfact`x'       = (cte_adv_high_fact`x' >= 1 & !missing(cte_adv_high_fact`x'))
  gen cte_conc_costfact`x'      = (cte_adv_high_fact`x' == 2)
  gen cte_part_costfact`x'      = (cte_adv_high_fact`x' == 1)
	drop cte_adv_high_fact`x'
}



/*
// CTE COMPLETION, CONCETRATION, AND PARTICIPATION IN A TESTED PROGRAM
gen tested_prog = inlist(cipcode, "43.1000", "47.0603", "47.0604", "47.0613", ///
  "51.0000", "52.0299", "52.0800", "52.1999")

by student (year cipcode): gegen comp_max_test_prog = max(completer * (test_prog == 1))
by student (year cipcode): gegen comp_max_non_test_prog = max(completer * (test_prog == 0))

gen byte comp_test_prog = (comp_max_test_prog == 3)
gen byte comp_non_test_prog = (comp_max_non_test_prog == 3)

gen byte conc_cuml_test_prog = (comp_max_test_prog >= 2 & !missing(comp_max_test_prog))
gen byte part_cuml_test_prog = (comp_max_test_prog >= 1 & !missing(comp_max_test_prog))

gen byte conc_excl_test_prog = (comp_max_test_prog == 2)
gen byte part_excl_test_prog = (comp_max_test_prog == 1)
*/

// FIRST AND LAST PARTICIPATION IN CTE
foreach x in year grade {
  bys student: gegen first_`x'_part = min(`x' if adv_p4 >= 1 & !missing(adv_p4))
  bys student: gegen first_`x'_conc = min(`x' if adv_p4 >= 2 & !missing(adv_p4))
  bys student: gegen first_`x'_comp = min(`x' if adv_p4 == 3)
}

forvalues x = 9/12 {
  gen part_by_g`x' = first_grade_part <= `x'
  gen conc_by_g`x' = first_grade_conc <= `x'
  gen comp_by_g`x' = first_grade_comp <= `x'
}

***
* Assessments
****

// ASSESSMENT AND SCORES IN MANDATED TESTS
foreach path in 430100 470603 470604 470613 510000 520299 520800 521999 {
	bys student: gegen took_test_prog`path' = max(assessment2==1 & pathway==`path')
	
	* TESTS ARE ON DIFFERENT SCALES
	sum assess_score if pathway==`path' & assessment2==1
	bys student: gegen test_score_prog`path' = max( (assess_score-`r(mean)')/`r(sd)' if pathway==`path')
}

// EVER TOOK MANDATED TEST AND BEST (STANDARDIZED) SCORE
egen cte_test_any = rowmax(took_test_prog*)
egen cte_best_score = rowmax(test_score_prog*)
assert  cte_best_score!=.  if cte_test_any==1



***
* Segments
****

//  MAKE MOST SEGMENTS
bys student: gegen max_segment = max(total_segments)
gen notmax_flag = total_segments!=max_segment
gen r = runiform()
bys student (notmax_flag r): gen max_segment_cip = cipcode[1]

drop notmax_flag 


***
* Courses
****

//  MAKE MOST Courses
bys student: gegen max_courses = max(course_count)
bys student: gegen max_semesters = max(total_semesters)

gen notmax_flag2 = total_semesters!=max_semesters | course_count!=max_courses 
bys student (notmax_flag r): gen max_semester_cip = cipcode[1]
bys student (notmax_flag r): gen max_course_cip = cipcode[1]

drop notmax_flag2 r

***
* Keep and Save
***

// KEEP VARIABLES
drop year grade cipcode pathway fedcluster adv_p4 total_segments total_mins ///
  cohort9 cte_adv_high cte_adv_high_cluster* cte_adv_high_prog*  progname-_merge ///
  assessment assessment2 assess_score assess_year test_cip segment13 course_count total_semesters ///
   comp_course course_complete courseless_complete

// ORDER VARIABLES
order student cte_any cte_conc_comp cte_comp cte_conc cte_part

// DROP DUPLICATES
gduplicates drop
gisid student

// FORMAT VARIABLES
format student %16.0f

// VARIABLE LABELS
label var student "Student ID"
label var cte_any "Any level of progression through a CTE pathway"
label var cte_conc_comp "Concentrated or completed a CTE pathway"
label var cte_comp "Completed a CTE pathway"
label var cte_conc "Concentrated in a CTE pathway"
label var cte_part "Participated in a CTE pathway"

label var cte_comp_cluster1 "Completed a CTE pathway in Agri., Food & Nat'l Resc's"
label var cte_conc_comp_cluster1 "Concentrated or completed a CTE pathway in Agri., Food & Nat'l Resc's"
label var cte_any_cluster1 "Any level of progression through a CTE pathway in Agri., Food & Nat'l Resc's"
label var cte_conc_cluster1 "Concentrated in a CTE pathway in Agri., Food & Nat'l Resc's"
label var cte_part_cluster1 "Participated in a CTE pathway in Agri., Food & Nat'l Resc's"

label var cte_comp_cluster2 "Completed a CTE pathway in Architecture & Constr."
label var cte_conc_comp_cluster2 "Concentrated or completed a CTE pathway in Architecture & Constr."
label var cte_any_cluster2 "Any level of progression through a CTE pathway in Architecture & Constr."
label var cte_conc_cluster2 "Concentrated in a CTE pathway in Architecture & Constr."
label var cte_part_cluster2 "Participated in a CTE pathway in Architecture & Constr."

label var cte_comp_cluster3 "Completed a CTE pathway in Arts, A/V Tech. & Comms."
label var cte_conc_comp_cluster3 "Concentrated or completed a CTE pathway in Arts, A/V Tech. & Comms."
label var cte_any_cluster3 "Any level of progression through a CTE pathway in Arts, A/V Tech. & Comms."
label var cte_conc_cluster3 "Concentrated in a CTE pathway in Arts, A/V Tech. & Comms."
label var cte_part_cluster3 "Participated in a CTE pathway in Arts, A/V Tech. & Comms."

label var cte_comp_cluster4 "Completed a CTE pathway in Bus. Mgmt. & Admin."
label var cte_conc_comp_cluster4 "Concentrated or completed a CTE pathway in Bus. Mgmt. & Admin."
label var cte_any_cluster4 "Any level of progression through a CTE pathway in Bus. Mgmt. & Admin."
label var cte_conc_cluster4 "Concentrated in a CTE pathway in Bus. Mgmt. & Admin."
label var cte_part_cluster4 "Participated in a CTE pathway in Bus. Mgmt. & Admin."

label var cte_comp_cluster5 "Completed a CTE pathway in Educ. & Training"
label var cte_conc_comp_cluster5 "Concentrated or completed a CTE pathway in Educ. & Training"
label var cte_any_cluster5 "Any level of progression through a CTE pathway in Educ. & Training"
label var cte_conc_cluster5 "Concentrated in a CTE pathway in Educ. & Training"
label var cte_part_cluster5 "Participated in a CTE pathway in Educ. & Training"

label var cte_comp_cluster6 "Completed a CTE pathway in Finance"
label var cte_conc_comp_cluster6 "Concentrated or completed a CTE pathway in Finance"
label var cte_any_cluster6 "Any level of progression through a CTE pathway in Finance"
label var cte_conc_cluster6 "Concentrated in a CTE pathway in Finance"
label var cte_part_cluster6 "Participated in a CTE pathway in Finance"

label var cte_comp_cluster7 "Completed a CTE pathway in Gov't & Pub. Admin."
label var cte_conc_comp_cluster7 "Concentrated or completed a CTE pathway in Gov't & Pub. Admin."
label var cte_any_cluster7 "Any level of progression through a CTE pathway in Gov't & Pub. Admin."
label var cte_conc_cluster7 "Concentrated in a CTE pathway in Gov't & Pub. Admin."
label var cte_part_cluster7 "Participated in a CTE pathway in Gov't & Pub. Admin."

label var cte_comp_cluster8 "Completed a CTE pathway in Health Sci."
label var cte_conc_comp_cluster8 "Concentrated or completed a CTE pathway in Health Sci."
label var cte_any_cluster8 "Any level of progression through a CTE pathway in Health Sci."
label var cte_conc_cluster8 "Concentrated in a CTE pathway in Health Sci."
label var cte_part_cluster8 "Participated in a CTE pathway in Health Sci."

label var cte_comp_cluster9 "Completed a CTE pathway in Hospitality & Tourism"
label var cte_conc_comp_cluster9 "Concentrated or completed a CTE pathway in Hospitality & Tourism"
label var cte_any_cluster9 "Any level of progression through a CTE pathway in Hospitality & Tourism"
label var cte_conc_cluster9 "Concentrated in a CTE pathway in Hospitality & Tourism"
label var cte_part_cluster9 "Participated in a CTE pathway in Hospitality & Tourism"

label var cte_comp_cluster10 "Completed a CTE pathway in Human Services"
label var cte_conc_comp_cluster10 "Concentrated or completed a CTE pathway in Human Services"
label var cte_any_cluster10 "Any level of progression through a CTE pathway in Human Services"
label var cte_conc_cluster10 "Concentrated in a CTE pathway in Human Services"
label var cte_part_cluster10 "Participated in a CTE pathway in Human Services"

label var cte_comp_cluster11 "Completed a CTE pathway in Information Tech."
label var cte_conc_comp_cluster11 "Concentrated or completed a CTE pathway in Information Tech."
label var cte_any_cluster11 "Any level of progression through a CTE pathway in Information Tech."
label var cte_conc_cluster11 "Concentrated in a CTE pathway in Information Tech."
label var cte_part_cluster11 "Participated in a CTE pathway in Information Tech."

label var cte_comp_cluster12 "Completed a CTE pathway in Law, Pub. Sfty., Correc. & Sec."
label var cte_conc_comp_cluster12 "Concentrated or completed a CTE pathway in Law, Pub. Sfty., Correc. & Sec."
label var cte_any_cluster12 "Any level of progression through a CTE pathway in Law, Pub. Sfty., Correc. & Sec."
label var cte_conc_cluster12 "Concentrated in a CTE pathway in Law, Pub. Sfty., Correc. & Sec."
label var cte_part_cluster12 "Participated in a CTE pathway in Law, Pub. Sfty., Correc. & Sec."

label var cte_comp_cluster13 "Completed a CTE pathway in Manufacturing"
label var cte_conc_comp_cluster13 "Concentrated or completed a CTE pathway in Manufacturing"
label var cte_any_cluster13 "Any level of progression through a CTE pathway in Manufacturing"
label var cte_conc_cluster13 "Concentrated in a CTE pathway in Manufacturing"
label var cte_part_cluster13 "Participated in a CTE pathway in Manufacturing"

label var cte_comp_cluster14 "Completed a CTE pathway in Marketing"
label var cte_conc_comp_cluster14 "Concentrated or completed a CTE pathway in Marketing"
label var cte_any_cluster14 "Any level of progression through a CTE pathway in Marketing"
label var cte_conc_cluster14 "Concentrated in a CTE pathway in Marketing"
label var cte_part_cluster14 "Participated in a CTE pathway in Marketing"

label var cte_comp_cluster15 "Completed a CTE pathway in STEM"
label var cte_conc_comp_cluster15 "Concentrated or completed a CTE pathway in STEM"
label var cte_any_cluster15 "Any level of progression through a CTE pathway in STEM"
label var cte_conc_cluster15 "Concentrated in a CTE pathway in STEM"
label var cte_part_cluster15 "Participated in a CTE pathway in STEM"

label var cte_comp_cluster16 "Completed a CTE pathway in Trans., Dist. & Logist."
label var cte_conc_comp_cluster16 "Concentrated or completed a CTE pathway in Trans., Dist. & Logist."
label var cte_any_cluster16 "Any level of progression through a CTE pathway in Trans., Dist. & Logist."
label var cte_conc_cluster16 "Concentrated in a CTE pathway in Trans., Dist. & Logist."
label var cte_part_cluster16 "Participated in a CTE pathway in Trans., Dist. & Logist."

ds cte_comp_prog*
foreach var in `r(varlist)' {
  label var `var' "Completed CTE pathway"
}

ds cte_conc_comp_prog*
foreach var in `r(varlist)' {
  label var `var' "Concentrated or completed CTE pathway"
}

ds cte_any_prog*
foreach var in `r(varlist)' {
  label var `var' "Any level of progression through CTE pathway"
}

ds cte_conc_prog*
foreach var in `r(varlist)' {
  label var `var' "Concentrated in CTE pathway"
}

ds cte_part_prog*
foreach var in `r(varlist)' {
  label var `var' "Participated in CTE pathway"
}

label var first_year_part "First year to participate in a CTE pathway"
label var first_year_conc "First year to concentrate in a CTE pathway"
label var first_year_comp "First year to complete a CTE pathway"
label var first_grade_part "First grade to participate in a CTE pathway"
label var first_grade_conc "First grade to concentrate in a CTE pathway"
label var first_grade_comp "First grade to complete a CTE pathway"

forvalues x = 9/12 {
  label var part_by_g`x' "Participated in a CTE pathway by Grade `x'"
  label var conc_by_g`x' "Concentrated in a CTE pathway by Grade `x'"
  label var comp_by_g`x' "Completed a CTE pathway by Grade `x'"
}

// VALUE LABELS

// Define value labels
label define yesno ///
  0 "No" ///
  1 "Yes"

// Label values
label values first_year_* year
label values first_grade_* grade
label values cte_* *_by_g* yesno

// SORT & SAVE
sort student
compress
save $Output/cte_advancement.dta, replace 
